﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MyFirstSolution.Model;
using MyFirstSolution.BLL;
namespace MyFirstSolution.UI
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 界面窗体使用的用户数据管理对象--BLL中实现
        /// </summary>
        private UserManager userManager;

        /// <summary>
        /// 存放所有用户数据的列表
        /// </summary>
        private List<User> Users;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
           
            dataGridView1_Load();
        }

        /// <summary>
        /// 加载dataGridView
        /// </summary>
        private void dataGridView1_Load()
        {
             userManager = new UserManager();
            Users = new List<User>();
            Users = userManager.GetAllUsers();
            //清空dataGridView1
            dataGridView1.Rows.Clear();


            dataGridView1.Columns.Add("ID", "ID");
            dataGridView1.Columns.Add("Account", "Account");
            dataGridView1.Columns.Add("Password", "Password");
            dataGridView1.Columns.Add("UserType", "UserType");
            for (int i = 0; i < Users.Count; i++)
            {
                int index = dataGridView1.Rows.Add();
                User user = Users[i];
                dataGridView1.Rows[index].Cells[0].Value = user.ID;
                dataGridView1.Rows[index].Cells[0].ReadOnly = true;
                dataGridView1.Rows[index].Cells[1].Value = user.Account;
                dataGridView1.Rows[index].Cells[2].Value = user.Password;
                dataGridView1.Rows[index].Cells[3].Value = user.UserType;
            }
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认删除吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                int a = dataGridView1.CurrentRow.Index;
                int ID = (int)dataGridView1.Rows[a].Cells[0].Value;
                if (userManager.DeleteUserInfo(ID))
                {
                    //重新加载dataGridView
                    dataGridView1_Load();
                    MessageBox.Show("删除成功！", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else MessageBox.Show("删除失败！", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Insert insert = new Insert();
            insert.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1_Load();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认修改吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                int a = dataGridView1.CurrentRow.Index;
                string account = dataGridView1.Rows[a].Cells[1].Value.ToString();
                string password = dataGridView1.Rows[a].Cells[2].Value.ToString();
                if (userManager.UpdateUsersPassword(account,password))
                {
                    //重新加载dataGridView  
                    dataGridView1_Load();
                    MessageBox.Show("修改成功！", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else MessageBox.Show("修改失败！", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

            
    }
}
